﻿using TiaoTiaoCode.NLogger;
using TiaoTiaoCode.NLogger.Dtos;
using TiaoTiaoCode.NLogger.Enums;

namespace TiaoTiaoCode.AutoUpdate.WebApi.StartUps
{
    /// <summary>
    /// NLog启动配置
    /// </summary>
    public static class NLogStartup
    {
        /// <summary>
        /// NLog配置启动
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        public static void AddNLogStartup(this IServiceCollection services, IConfiguration config)
        {
            var dbTypeCfg = config.GetValue<string>("DbType");

            var nDbType = dbTypeCfg switch
            {
                "MySql" => NLogDbType.MySql,
                "SqlServer" => NLogDbType.SqlServer,
                "Sqlite" => NLogDbType.Sqlite,
                _ => NLogDbType.Sqlite,
            };

            //TiaoTiaoNLogger.AddInfoFileTarget();

            //TiaoTiaoNLogger.AddDatabaseTarget(
            //            nDbType,
            //            config.GetConnectionString(nDbType.ToString()),
            //            $@"insert into sys_log(id,log_data,method,table_name,log_date,operate_type) values(@Id,@LogData,@Method,@TableName,@LogDate,@OperateType)",
            //            typeof(DbLogItem));

            ////var cfg = TiaoTiaoNLogger.GetConfig(ft, null, dt, null);

            ////TiaoTiaoNLogger.InitLogger(cfg);
            //TiaoTiaoNLogger.InitLogger();

            TiaoTiaoNLogger.AddFileTarget(TiaoTiaoNLogger.DefaultLoggerName, "info", NLog.LogLevel.Info);
            TiaoTiaoNLogger.AddDatabaseTarget(TiaoTiaoNLogger.DefaultLoggerName,
                        nDbType,
                        config.GetConnectionString(nDbType.ToString()),
                        $@"insert into sys_log(id,log_data,method,table_name,log_date,operate_type) values(@Id,@LogData,@Method,@TableName,@LogDate,@OperateType)",
                        typeof(DbLogItem));

            TiaoTiaoNLogger.SetLogger(TiaoTiaoNLogger.DefaultLoggerName);
        }
    }
}
